package com.atguigu.flink.chapter05.source;

import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/*
     从文件读取数据
 */
public class FileSourceDemo {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port",20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        //创建一个file source: 先获取xxxBuilder,再build()可以得到对应的source
        FileSource<String> source = FileSource
                .forRecordStreamFormat(new TextLineInputFormat(), new Path("input/words.txt"))
                .build();
        DataStreamSource<String> stream = env.fromSource(
                source,
                WatermarkStrategy.noWatermarks(),
                "file-source"
        );

        stream.print();

        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

//public class FileSourceDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        //创建一个FileSource: 先获取xxxBuilder(),再通过 .build得到对应的source
//        FileSource<String> source = FileSource
//                .forRecordStreamFormat(new TextLineInputFormat(), new Path("input/words.txt"))
//                .build();
//        DataStreamSource<String> stream = env.fromSource(
//                source,
//                WatermarkStrategy.noWatermarks(),
//                "file-source"
//        );
//
//        stream.print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}


//public class FileSourceDemo {
//    public static void main(String[] args) {
//        Configuration conf = new Configuration();
//        conf.setInteger("rest.port",20000);
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
//        env.setParallelism(1);
//
//        //创建一个FileSource: 先获取xxxBuilder,再通过 .build()得到对应的source
//        FileSource<String> source = FileSource
//                .forRecordStreamFormat(new TextLineInputFormat(), new Path("C:\\IDEA\\code\\LearnDemo\\flink\\input\\words.txt"))
//                .build();
//        DataStreamSource<String> stream = env.fromSource(
//                source,
//                WatermarkStrategy.noWatermarks(),
//                "file-source"
//        );
//
//        stream.print();
//        try {
//            env.execute();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//}